package com.treeroot.treenet.config;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Description: 配置工具
 * Create By TreeRoot
 * Create Time: 2025/06/02 08:02
 * Class Name: ConfigUtil
 *
 * @author TreeRoot
 */
@Slf4j
public class ConfigUtil {

    private static final String CONFIG_FILE = "config.properties";
    private static final int DEFAULT_TCP_PORT = 9999;

    private static final Properties PROPERTIES = new Properties();

    static {
        try (InputStream input = ConfigUtil.class.getClassLoader().getResourceAsStream(CONFIG_FILE)) {
            if (input != null) {
                PROPERTIES.load(input);
            }
        } catch (IOException e) {
            log.error("Error loading properties from " + CONFIG_FILE, e);
        }
    }

    public static int getTcpPort() {
        String portStr = PROPERTIES.getProperty("tcp.port");
        if (portStr != null) {
            try {
                return Integer.parseInt(portStr);
            } catch (NumberFormatException ignored) {}
        }
        return DEFAULT_TCP_PORT;
    }
}